﻿@using ALinq.BBS.Models
@{
    var pagingModel = (ALinq.BBS.Models.PagingModel)this.PageData.Values.ToArray()[0];

    int page;
    int remain;
    page = Math.DivRem(pagingModel.PageIndex, pagingModel.PagingSize, out remain);
    var star = page * pagingModel.PagingSize;


    //page = Math.DivRem(pagingModel.PageIndex, pagingModel.PagingSize, out remain);
    int page1;
    int remain1;
    page1 = Math.DivRem(pagingModel.PagesCount, pagingModel.PagingSize, out remain1);
    int end = (page + 1) * pagingModel.PagingSize - 1;
    int end1 = page1 * pagingModel.PagingSize + remain1 - 1;
    end = end < end1 ? end : end1;

    string firstUrl = null;
    if (pagingModel.PageIndex > 0)
    {
        firstUrl = pagingModel.CreateUrl(0);
    }

    string lastUrl = null;
    if (pagingModel.PageIndex < pagingModel.PagesCount - 1)
    {
        lastUrl = pagingModel.CreateUrl(pagingModel.PagesCount - 1);
    }

    page = Math.DivRem(pagingModel.PageIndex, pagingModel.PagingSize, out remain);
    string previousUrl = null;
    if (page != 0)
    {
        previousUrl = pagingModel.CreateUrl((page - 1) * pagingModel.PagingSize);
    }

    //计算最后一页的页码
    int last = pagingModel.PagesCount - 1;
    string nextUrl = null;
    if (page != last)
    {
        nextUrl = pagingModel.CreateUrl((page + 1) * pagingModel.PagingSize);
    }
}
<div style="text-align: right; vertical-align: middle; font-size: 12px">
    共<b>@pagingModel.RecordsCount</b>条记录， 共<b>@pagingModel.PagesCount</b>页
    @if (firstUrl != null)
    {
        <a href="@firstUrl">
            <img alt="首页" src="@Url.Content("~/Content/Images/Button/ArrowFirst.gif")" /></a>
    }
    @if (previousUrl != null)
    {
        <a href="@previousUrl">
            <img alt="前十页" src="@Url.Content("~/Content/Images/Button/ArrowPrevious.gif")" />
        </a>      
    }
    <span>
        @for (int i = star; i <= end; i++)
        {
            if (i == pagingModel.PageIndex)
            { 
            <b style="font-size: 14px">@(i + 1)</b>
            }
            else
            {  
            <a href="@pagingModel.CreateUrl(i)">@(i + 1)
            </a> 
            }
        }
    </span>
    @if (nextUrl != null)
    {
        <a href="@nextUrl">
            <img alt="下十页" src="@Url.Content("~/Content/Images/Button/ArrowNext.gif")" /></a>
    }
    @if (lastUrl != null)
    {
        <a href="@lastUrl">
            <img alt="未页" src="@Url.Content("~/Content/Images/Button/ArrowLast.gif")" /></a>
    }
    <input type="text" id="txtPageNumber" style="width: 26px; font-size: 14px; padding: 0px 0px 0px 0px;
        border: groove thin" onkeydown="Paging_OnKeyDown(this)" tabindex="1" />&nbsp;
    <a href="javascript:Paging_Go()">
        <img alt="确定" src="@Url.Content("~/Content/Images/Button/ButtonOK.gif")" />
    </a>
</div>
<script type="text/javascript">
    function Paging_GoTo(pageNumber) {
        var url = "@pagingModel.CreateUrl(-9999)";
        var count = @pagingModel.PagesCount;
        if (pageNumber > count-1)
            pageNumber = count-1;
        if (pageNumber < 0)
            pageNumber = 0;
        url = url.replace('-9999', pageNumber - 1);
        window.open(url, "_self");
    }
    function Paging_OnKeyDown(object) {
        if (object.id == "txtPageNumber" && window.event.keyCode == 13) {
            var pageNumber = new Number(object.value);
            Paging_GoTo(pageNumber);
        }
    }
    function Paging_Go() {
        var objects = document.getElementsByName("txtPageNumber");
        for (i = 0; i < objects.length; i++) {
            var object = objects[i];
            if (object.value != null) {
                var pageNumber = new Number(object.value);
                Paging_GoTo(pageNumber);
            }
        }
    }
</script>
